
package org.nmgyjt.springboot.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import org.nmgyjt.springboot.beans.admin.CsSystemParIDSelectBean;
import org.nmgyjt.springboot.entity.ApiRes;
import org.nmgyjt.springboot.entity.area.Area;
import org.nmgyjt.springboot.service.IAreaService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.Collection;
import java.util.List;

@RestController
@RequestMapping("/cs/china")
public class AreaController {

    @Autowired
    private IAreaService service;

    @GetMapping("/getById")
    public ApiRes getById(String id) {
        return ApiRes.ok(service.getOne(new LambdaQueryWrapper<Area>().eq(Area::getId, id)));
    }

    @GetMapping("/getByName")
    public ApiRes getByName(@RequestParam String name) {
        return ApiRes.ok(service.list(new LambdaQueryWrapper<Area>().like(Area::getName, name)));
    }

    @PostMapping("/getByParId")
    public ApiRes getByParId(@RequestBody CsSystemParIDSelectBean bean) {
        if (StringUtils.isNotBlank(bean.getLevel()) && "1".equals(bean.getLevel())) {

            List<Area> objects = new ArrayList<>();
            for (Area area : service.list(new LambdaQueryWrapper<Area>().eq(Area::getParId, bean.getParId()))) {
                List<Area> areas = service.list(new LambdaQueryWrapper<Area>().eq(Area::getParId, area.getId()));
                if (CollectionUtils.isNotEmpty(areas)) {
                    objects.addAll(areas);
                }
            }
            return ApiRes.ok(objects);
        }
        return ApiRes.ok(service.list(new LambdaQueryWrapper<Area>().eq(Area::getParId, bean.getParId())));
    }

    @GetMapping("/getByRemark")
    public ApiRes getByRemark(String remark) {
        return ApiRes.ok(service.list(new LambdaQueryWrapper<Area>().eq(Area::getRemark, remark)));
    }

    @PostMapping("/getByArea")
    public ApiRes getByArea(@RequestBody Area area) {
        LambdaQueryWrapper<Area> wrapper = new LambdaQueryWrapper<Area>()
                .eq(StringUtils.isNotBlank(area.getId()), Area::getId, area.getId())
                .like(StringUtils.isNotBlank(area.getName()), Area::getName, area.getName())
                .eq(StringUtils.isNotBlank(area.getParId()), Area::getParId, area.getParId())
                .eq(StringUtils.isNotBlank(area.getRemark()), Area::getRemark, area.getRemark());
        return ApiRes.ok(service.list(wrapper));
    }
}

